"""
@Author  : 康帅
@Time    : 2021/8/5 16:59
@Function: 力扣：5.最长回文子串(中等)
"""


class Solution(object):

    def longestPalindrome(self, s):
        """
        使用dp动态规划解题
        :type s: str
        :rtype: str
        """
        palindrome = ""

        for i in range(len(s)):
            len1 = len(self.help_method(s, i, i))

            if len1 > len(palindrome):
                palindrome = self.help_method(s, i, i)

            len2 = len(self.help_method(s, i, i + 1))

            if len2 > len(palindrome):
                palindrome = self.help_method(s, i, i + 1)

        return palindrome

    def help_method(self, s, left, right):
        """
        此方法作为辅助函数
        :param s:
        :param left:
        :param right:
        :return:
        """
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        return s[left + 1: right]


if __name__ == '__main__':
    data = Solution()
    print(data.longestPalindrome("cbbd"))
